package com.fzrskj.cloud.free.core.utils;

import com.fzrskj.cloud.free.sys.bean.SysAgentRelationBean;
import com.fzrskj.cloud.free.sys.dao.SysAgentRelationDao;
import org.apache.commons.collections.map.HashedMap;

import java.util.Map;
import java.util.Objects;

/**
 * AgentDataRangeUtils.
 *
 * @author zj.
 *         Created on 2019/9/10 0010.
 */
public class AgentDataRangeUtils {
    /**
     * 代理数据隔离.
     * @param currentAgentId 当前代理ID.
     * @param sql sql.
     * @param column range column.
     * @return sql.
     */
    public static String formatDataRange(Integer currentAgentId, String sql, String column) {
        if (currentAgentId < 1) {
            return sql;
        }

        SysAgentRelationDao relationDao = new SysAgentRelationDao();
        Map<String, Object> cdt = new HashedMap();
        cdt.put("agentId", currentAgentId);
        SysAgentRelationBean relationBean = relationDao.queryOneSysAgentRelation(cdt);
        String agentIdIn = String.valueOf(currentAgentId);
        if (null != relationBean) {
            agentIdIn += relationBean.getSubIdList();
        }

        sql += " and " + column + " in (" + agentIdIn + ")";
        return sql;
    }

    /**
     * 代理数据隔离.
     * @param currentAgentId 当前代理ID.
     * @param sql sql.
     * @return sql.
     */
    public static String formatDataRange(Integer currentAgentId, String sql) {
        return formatDataRange(currentAgentId, sql, "agentId");
    }

    /**
     * 代理数据隔离.
     * @param currentAgentId  当前代理ID.
     * @param sql source sql.
     * @param column link column.
     * @return sql.
     */
    public static StringBuilder formatDataRange(Integer currentAgentId, StringBuilder sql, String column) {
        if (currentAgentId < 1) {
            return sql;
        }

        SysAgentRelationDao relationDao = new SysAgentRelationDao();
        Map<String, Object> cdt = new HashedMap();
        cdt.put("agentId", currentAgentId);
        SysAgentRelationBean relationBean = relationDao.queryOneSysAgentRelation(cdt);
        String agentIdIn = String.valueOf(currentAgentId);
        if (null != relationBean) {
            agentIdIn += relationBean.getSubIdList();
        }

        sql.append(" and " + column + " in (" + agentIdIn + ")");
        return sql;
    }

    /**
     * 代理数据隔离.
     * @param currentAgentId 当前代理ID.
     * @param sql source sql.
     * @return sql.
     */
    public static StringBuilder formatDataRange(Integer currentAgentId, StringBuilder sql) {
        return formatDataRange(currentAgentId, sql, "agentId");
    }

    public static void formateDateRange(Integer currentAgentId, Map<String, Objects> cdt) {

    }
}
